/**
 * Copyright (c) 2018, wuyy All rights reserved.
 * wuyy 专有/保密源代码,未经许可禁止任何人通过任何渠道使用、修改源代码.
 * QQ:16349023
 * 项目名称 : com.wy2.eclipse.tool.ui
 * @version V1.0
 */
package com.wy2.eclipse.tool.ui.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wy2.eclipse.tool.lib.ProjectConfigManager;
import com.wy2.eclipse.tool.lib.common.Constants;
import com.wy2.eclipse.tool.lib.common.IDEUtils;
import com.wy2.eclipse.tool.ui.UIActivator;
import com.wy2.eclipse.tool.ui.menuadapter.ProjectAdapter;

/**
 * @author wuyy<16349023@qq.com>
 * @date 2018年1月17日下午3:31:30
 * 
 */
public class CreateConfigAction implements IObjectActionDelegate {
    
    private static final Logger logger = LoggerFactory.getLogger(CreateConfigAction.class);

    private IWorkbenchPart targetPart;
    private IProject selectProject;
    private ISelection selection;

    /** 
     * @param action 
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    @Override
    public void run(IAction action) {
        logger.info("name:{}, localtion: {} ,创建配置", selectProject.getName(),selectProject.getLocation().toOSString());
        
        ProjectConfigManager projectConfigManager=new ProjectConfigManager(selectProject, Constants.GENERATE_CONFIG_NAME);
        projectConfigManager.createGenerateConfig();
        
//        String configPath=Constants.CONFIG_TEMPLATE_DIRECTORY +"/"+Constants.GENERATE_CONFIG_NAME;
//        File configFile=new File(selectProject.getLocation().toFile(),Constants.GENERATE_CONFIG_NAME);
//        try(InputStream inputStream = UIActivator.getDefault().getBundle().getEntry(configPath).openStream();
//                FileOutputStream fos = new FileOutputStream(configFile)){
//            for(byte[] buffer = new byte[1024 * 4];;) {
//                int n = inputStream.read(buffer);
//                if(n < 0) {
//                  break;
//                }
//                fos.write(buffer, 0, n);
//              }
//            fos.flush();
//            
//            
//            //IDEUtils.refreshFile(configFile.getAbsolutePath());
//            logger.info("生成配置创建成功，位置： {}",configFile.getAbsolutePath());
//        } catch (IOException e) {
//            logger.error("创建生成配置异常", e);
//        }
//        IDEUtils.refreshProject(selectProject);

    }

    /** 
     * @param action
     * @param selection 
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        this.selection=selection;
        
        StructuredSelection ss = (StructuredSelection) selection;
        ProjectAdapter adapter = (ProjectAdapter) ss.getFirstElement();
        if (adapter != null) {
            selectProject = adapter.getProject();
        }

    }

    /** 
     * @param action
     * @param targetPart 
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        this.targetPart=targetPart;

    }

}
